// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.BodyDamage;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import se.krka.kahlua.j2se.KahluaTableImpl;
import zombie.Lua.LuaManager;
import zombie.characters.IsoGameCharacter;
import zombie.characters.Moodles.MoodleType;
import zombie.characters.skills.PerkFactory;
import zombie.core.Rand;

public final class Fitness {
    private IsoGameCharacter parent = null;
    private HashMap<String, Float> regularityMap = new HashMap();
    private int fitnessLvl = 0;
    private int strLvl = 0;
    private final HashMap<String, Integer> stiffnessTimerMap = new HashMap();
    private final HashMap<String, Float> stiffnessIncMap = new HashMap();
    private final ArrayList<String> bodypartToIncStiffness = new ArrayList();
    private final HashMap<String, Fitness.FitnessExercise> exercises = new HashMap();
    private final HashMap<String, Long> exeTimer = new HashMap();
    private int lastUpdate = -1;
    private Fitness.FitnessExercise currentExe;
    private static final int HOURS_FOR_STIFFNESS = 12;
    private static final float BASE_STIFFNESS_INC = 0.5F;
    private static final float BASE_ENDURANCE_RED = 0.015F;
    private static final float BASE_REGULARITY_INC = 0.08F;
    private static final float BASE_REGULARITY_DEC = 0.002F;
    private static final float BASE_PAIN_INC = 2.5F;

    public Fitness(IsoGameCharacter _parent) {
        this.setParent(_parent);
    }

    public void update() {
        int int0 = zombie.GameTime.getInstance().getMinutes() / 10;
        if (this.lastUpdate == -1) {
            this.lastUpdate = int0;
        }

        if (int0 != this.lastUpdate) {
            this.lastUpdate = int0;
            ArrayList arrayList = new ArrayList();
            this.decreaseRegularity();

            for (String string0 : this.stiffnessTimerMap.keySet()) {
                Integer integer = (Integer)this.stiffnessTimerMap.get(string0);
                integer = integer - 1;
                if (integer <= 0) {
                    arrayList.add(string0);
                    this.bodypartToIncStiffness.add(string0);
                } else {
                    this.stiffnessTimerMap.put(string0, integer);
                }
            }

            for (int int1 = 0; int1 < arrayList.size(); int1++) {
                this.stiffnessTimerMap.remove(arrayList.get(int1));
            }

            for (int int2 = 0; int2 < this.bodypartToIncStiffness.size(); int2++) {
                String string1 = (String)this.bodypartToIncStiffness.get(int2);
                Float _float = (Float)this.stiffnessIncMap.get(string1);
                if (_float == null) {
                    return;
                }

                _float = _float - 1.0F;
                this.increasePain(string1);
                if (_float <= 0.0F) {
                    this.bodypartToIncStiffness.remove(int2);
                    this.stiffnessIncMap.remove(string1);
                    int2--;
                } else {
                    this.stiffnessIncMap.put(string1, _float);
                }
            }
        }
    }

    private void decreaseRegularity() {
        for (String string : this.regularityMap.keySet()) {
            if (this.exeTimer.containsKey(string)
                && zombie.GameTime.getInstance().getCalender().getTimeInMillis() - (Long)this.exeTimer.get(string) > 86400000L) {
                float _float = (Float)this.regularityMap.get(string);
                _float -= 0.002F;
                this.regularityMap.put(string, _float);
            }
        }
    }

    private void increasePain(String string) {
        if ("arms".equals(string)) {
            for (int int0 = BodyPartType.ForeArm_L.index(); int0 < BodyPartType.UpperArm_R.index() + 1; int0++) {
                BodyPart bodyPart0 = this.parent.getBodyDamage().getBodyPart(BodyPartType.FromIndex(int0));
                bodyPart0.setStiffness(bodyPart0.getStiffness() + 2.5F);
            }
        }

        if ("legs".equals(string)) {
            for (int int1 = BodyPartType.UpperLeg_L.index(); int1 < BodyPartType.LowerLeg_R.index() + 1; int1++) {
                BodyPart bodyPart1 = this.parent.getBodyDamage().getBodyPart(BodyPartType.FromIndex(int1));
                bodyPart1.setStiffness(bodyPart1.getStiffness() + 2.5F);
            }
        }

        if ("chest".equals(string)) {
            BodyPart bodyPart2 = this.parent.getBodyDamage().getBodyPart(BodyPartType.Torso_Upper);
            bodyPart2.setStiffness(bodyPart2.getStiffness() + 2.5F);
        }

        if ("abs".equals(string)) {
            BodyPart bodyPart3 = this.parent.getBodyDamage().getBodyPart(BodyPartType.Torso_Lower);
            bodyPart3.setStiffness(bodyPart3.getStiffness() + 2.5F);
        }
    }

    public void setCurrentExercise(String type) {
        this.currentExe = (Fitness.FitnessExercise)this.exercises.get(type);
    }

    public void exerciseRepeat() {
        this.fitnessLvl = this.parent.getPerkLevel(PerkFactory.Perks.Fitness);
        this.strLvl = this.parent.getPerkLevel(PerkFactory.Perks.Strength);
        this.incRegularity();
        this.reduceEndurance();
        this.incFutureStiffness();
        this.incStats();
        this.updateExeTimer();
    }

    private void updateExeTimer() {
        this.exeTimer.put(this.currentExe.type, zombie.GameTime.getInstance().getCalender().getTimeInMillis());
    }

    public void incRegularity() {
        float _floatx = 0.08F;
        byte _byte = 4;
        double _double = Math.log((double)((float)this.fitnessLvl / 5.0F + (float)_byte));
        _floatx = (float)((double)_floatx * (Math.log((double)(_byte + 1)) / _double));
        Float _floatx = (Float)this.regularityMap.get(this.currentExe.type);
        if (_floatx == null) {
            _floatx = 0.0F;
        }

        _floatx = _floatx + _floatx;
        _floatx = Math.min(Math.max(_floatx, 0.0F), 100.0F);
        this.regularityMap.put(this.currentExe.type, _floatx);
    }

    public void reduceEndurance() {
        float _floatx = 0.015F;
        Float _floatx = (Float)this.regularityMap.get(this.currentExe.type);
        if (_floatx == null) {
            _floatx = 0.0F;
        }

        byte _byte = 50;
        double _double = Math.log((double)(_floatx / 50.0F + (float)_byte));
        _floatx = (float)((double)_floatx * (_double / Math.log((double)(_byte + 1))));
        if (this.currentExe.metabolics == Metabolics.FitnessHeavy) {
            _floatx *= 1.3F;
        }

        _floatx *= (float)(1 + this.parent.getMoodles().getMoodleLevel(MoodleType.HeavyLoad) / 3);
        this.parent.getStats().setEndurance(this.parent.getStats().getEndurance() - _floatx);
    }

    public void incFutureStiffness() {
        Float float0 = (Float)this.regularityMap.get(this.currentExe.type);
        if (float0 == null) {
            float0 = 0.0F;
        }

        for (int _int = 0; _int < this.currentExe.stiffnessInc.size(); _int++) {
            float _float = 0.5F;
            String string = (String)this.currentExe.stiffnessInc.get(_int);
            if (!this.stiffnessTimerMap.containsKey(string) && !this.bodypartToIncStiffness.contains(string)) {
                this.stiffnessTimerMap.put(string, 72);
            }

            Float float1 = (Float)this.stiffnessIncMap.get(string);
            if (float1 == null) {
                float1 = 0.0F;
            }

            _float *= (120.0F - float0) / 170.0F;
            if (this.currentExe.metabolics == Metabolics.FitnessHeavy) {
                _float *= 1.3F;
            }

            _float *= (float)(1 + this.parent.getMoodles().getMoodleLevel(MoodleType.Tired) / 3);
            float1 = float1 + _float;
            float1 = Math.min(float1, 150.0F);
            this.stiffnessIncMap.put(string, float1);
        }
    }

    public void incStats() {
        float float0 = 0.0F;
        float float1 = 0.0F;

        for (int _int = 0; _int < this.currentExe.stiffnessInc.size(); _int++) {
            String string = (String)this.currentExe.stiffnessInc.get(_int);
            if ("arms".equals(string)) {
                float0 += 4.0F;
            }

            if ("chest".equals(string)) {
                float0 += 2.0F;
            }

            if ("legs".equals(string)) {
                float1 += 4.0F;
            }

            if ("abs".equals(string)) {
                float1 += 2.0F;
            }
        }

        if (this.strLvl > 5) {
            float0 *= (float)(1 + (this.strLvl - 5) / 10);
        }

        if (this.fitnessLvl > 5) {
            float1 *= (float)(1 + (this.fitnessLvl - 5) / 10);
        }

        float0 *= this.currentExe.xpModifier;
        float1 *= this.currentExe.xpModifier;
        this.parent.getXp().AddXP(PerkFactory.Perks.Strength, float0);
        this.parent.getXp().AddXP(PerkFactory.Perks.Fitness, float1);
    }

    public void resetValues() {
        this.stiffnessIncMap.clear();
        this.stiffnessTimerMap.clear();
        this.regularityMap.clear();
    }

    public void removeStiffnessValue(String type) {
        this.stiffnessIncMap.remove(type);
        this.stiffnessTimerMap.remove(type);
    }

    public void save(ByteBuffer output) {
        output.putInt(this.stiffnessIncMap.size());

        for (String string0 : this.stiffnessIncMap.keySet()) {
            zombie.GameWindow.WriteString(output, string0);
            output.putFloat((Float)this.stiffnessIncMap.get(string0));
        }

        output.putInt(this.stiffnessTimerMap.size());

        for (String string1 : this.stiffnessTimerMap.keySet()) {
            zombie.GameWindow.WriteString(output, string1);
            output.putInt((Integer)this.stiffnessTimerMap.get(string1));
        }

        output.putInt(this.regularityMap.size());

        for (String string2 : this.regularityMap.keySet()) {
            zombie.GameWindow.WriteString(output, string2);
            output.putFloat((Float)this.regularityMap.get(string2));
        }

        output.putInt(this.bodypartToIncStiffness.size());

        for (int _int = 0; _int < this.bodypartToIncStiffness.size(); _int++) {
            zombie.GameWindow.WriteString(output, (String)this.bodypartToIncStiffness.get(_int));
        }

        output.putInt(this.exeTimer.size());

        for (String string3 : this.exeTimer.keySet()) {
            zombie.GameWindow.WriteString(output, string3);
            output.putLong((Long)this.exeTimer.get(string3));
        }
    }

    public void load(ByteBuffer input, int WorldVersion) {
        if (WorldVersion >= 167) {
            int int0 = input.getInt();
            if (int0 > 0) {
                for (int int1 = 0; int1 < int0; int1++) {
                    this.stiffnessIncMap.put(zombie.GameWindow.ReadString(input), input.getFloat());
                }
            }

            int0 = input.getInt();
            if (int0 > 0) {
                for (int int2 = 0; int2 < int0; int2++) {
                    this.stiffnessTimerMap.put(zombie.GameWindow.ReadString(input), input.getInt());
                }
            }

            int0 = input.getInt();
            if (int0 > 0) {
                for (int int3 = 0; int3 < int0; int3++) {
                    this.regularityMap.put(zombie.GameWindow.ReadString(input), input.getFloat());
                }
            }

            int0 = input.getInt();
            if (int0 > 0) {
                for (int int4 = 0; int4 < int0; int4++) {
                    this.bodypartToIncStiffness.add(zombie.GameWindow.ReadString(input));
                }
            }

            if (WorldVersion >= 169) {
                int0 = input.getInt();
                if (int0 > 0) {
                    for (int int5 = 0; int5 < int0; int5++) {
                        this.exeTimer.put(zombie.GameWindow.ReadString(input), input.getLong());
                    }
                }
            }
        }
    }

    public boolean onGoingStiffness() {
        return !this.bodypartToIncStiffness.isEmpty();
    }

    public int getCurrentExeStiffnessTimer(String type) {
        type = type.split(",")[0];
        return this.stiffnessTimerMap.get(type) != null ? (Integer)this.stiffnessTimerMap.get(type) : 0;
    }

    public float getCurrentExeStiffnessInc(String type) {
        type = type.split(",")[0];
        return this.stiffnessIncMap.get(type) != null ? (Float)this.stiffnessIncMap.get(type) : 0.0F;
    }

    public IsoGameCharacter getParent() {
        return this.parent;
    }

    public void setParent(IsoGameCharacter _parent) {
        this.parent = _parent;
    }

    public float getRegularity(String type) {
        Float _float = (Float)this.regularityMap.get(type);
        if (_float == null) {
            _float = 0.0F;
        }

        return _float;
    }

    public HashMap<String, Float> getRegularityMap() {
        return this.regularityMap;
    }

    public void setRegularityMap(HashMap<String, Float> _regularityMap) {
        this.regularityMap = _regularityMap;
    }

    public void init() {
        if (this.exercises.isEmpty()) {
            KahluaTableImpl kahluaTableImpl0 = (KahluaTableImpl)LuaManager.env.rawget("FitnessExercises");
            KahluaTableImpl kahluaTableImpl1 = (KahluaTableImpl)kahluaTableImpl0.rawget("exercisesType");

            for (Entry entry : kahluaTableImpl1.delegate.entrySet()) {
                this.exercises.put((String)entry.getKey(), new Fitness.FitnessExercise((KahluaTableImpl)entry.getValue()));
            }

            this.initRegularityMapProfession();
        }
    }

    public void initRegularityMapProfession() {
        if (this.regularityMap.isEmpty()) {
            boolean boolean0 = false;
            boolean boolean1 = false;
            boolean boolean2 = false;
            if (this.parent.getDescriptor().getProfession().equals("fitnessInstructor")) {
                boolean1 = true;
            }

            if (this.parent.getDescriptor().getProfession().equals("fireofficer")) {
                boolean0 = true;
            }

            if (this.parent.getDescriptor().getProfession().equals("securityguard")) {
                boolean2 = true;
            }

            if (boolean0 || boolean1 || boolean2) {
                Iterator iterator = this.exercises.keySet().iterator();

                while (iterator.hasNext()) {
                    float _float = (float)Rand.Next(7, 12);
                    if (boolean0) {
                        _float = (float)Rand.Next(10, 20);
                    }

                    if (boolean1) {
                        _float = (float)Rand.Next(40, 60);
                    }

                    this.regularityMap.put((String)iterator.next(), _float);
                }
            }
        }
    }

    public static final class FitnessExercise {
        String type = null;
        Metabolics metabolics = null;
        ArrayList<String> stiffnessInc = null;
        float xpModifier = 1.0F;

        public FitnessExercise(KahluaTableImpl kahluaTableImpl) {
            this.type = kahluaTableImpl.rawgetStr("type");
            this.metabolics = (Metabolics)kahluaTableImpl.rawget("metabolics");
            this.stiffnessInc = new ArrayList(Arrays.asList(kahluaTableImpl.rawgetStr("stiffness").split(",")));
            if (kahluaTableImpl.rawgetFloat("xpMod") > 0.0F) {
                this.xpModifier = kahluaTableImpl.rawgetFloat("xpMod");
            }
        }
    }
}
